--败嗣
--每回合限一次，你可以将一名角色的一张手牌当【杀】或【闪】打出，然后本回合下次使用/打出牌的角色摸/弃置两张牌。
local baisi = fk.CreateSkill{
  name = "doublez__baisi"
}

Fk:loadTranslationTable{
  ["doublez__baisi"] = "败嗣",
  [":doublez__baisi"] = "每回合限一次，你可以将一名角色的一张手牌当【杀】或【闪】打出，然后本回合下次使用/打出牌的角色摸/弃置两张牌。",

  ["#doublez__baisi"] = "败嗣：你可以将一张牌当【杀】或【闪】打出，或不选择牌将其他角色的一张牌当【杀】或【闪】打出",
  ["#doublez__baisi_choosePlayer"] = "败嗣：选择一名其他角色",
  ["#doublez__baisi_chooseCard"] = "败嗣：将 %dest 的一张牌当 【%arg】 打出",
  ["#doublez__baisi_discard"] = "败嗣：你须弃置两张牌",

  ["$doublez__baisi1"] = "若依吾计而行，许昌旦夕可破！",
  ["$doublez__baisi2"] = "先锋怯战，非谋策之过。",
}

baisi:addEffect("viewas", {
  prompt = "#doublez__baisi",
  pattern = "slash,jink",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  interaction = function (self, player)
    return UI.CardNameBox{
      choices = player:getViewAsCardNames(baisi.name, {"slash", "jink"})
    }
  end,
  view_as = function (self, player, cards)
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = baisi.name
    if #cards == 1 then
      c:addSubcards(cards)
      return c
    else
      if table.find(Fk:currentRoom().alive_players,function (t)
        return t~=player and not t:isNude()
      end) then
        return c
      end
    end
  end,
  before_use = function (self, player, use)
    if #use.card.subcards == 0 then
      local room = player.room
      local targets = table.filter(Fk:currentRoom().alive_players,function (t)
        return t~=player and not t:isNude()
      end)
      local target = room:askToChoosePlayers(player, {
        targets = targets,
        skill_name = baisi.name,
        prompt = "#doublez__baisi_choosePlayer",
        min_num = 1,
        max_num = 1,
      })
      if #target == 0 then return baisi.name end
      local c = room:askToChooseCard(player, {
        target = target[1],
        skill_name = baisi.name,
        prompt = "#doublez__baisi_chooseCard::"..player.id..":"..self.interaction.data,
        flag = "he"
      })
      use.card:addSubcard(c)
    end
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function (self, player, response)
    return player:usedSkillTimes(baisi.name) < 1 and response
  end
})

baisi:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    local room = player.room
    local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    return player:usedSkillTimes(baisi.name) > 0 and player:usedEffectTimes(self.name) < player:usedSkillTimes(baisi.name) 
      and player:hasSkill(baisi.name) 
      and (player:usedSkillTimes(baisi.name) - player:usedEffectTimes(self.name) > #room.logic:getEventsByRule(GameEvent.SkillEffect, 999, function (e)
        return e.data.skill.name == baisi.name
      end, use.id))
    end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    target:drawCards(2, baisi.name)
  end
})

baisi:addEffect(fk.CardRespondFinished, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:usedSkillTimes(baisi.name) > 0 and player:usedEffectTimes(self.name) < player:usedSkillTimes(baisi.name) 
      and player:hasSkill(baisi.name)
      and (player:usedSkillTimes(baisi.name) - player:usedEffectTimes(self.name) > 1 or not table.contains( data.card.skillNames, baisi.name))
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    if target:isNude() then return end
    local num = math.min(#target:getCardIds("he"), 2)
    player.room:askToDiscard(target, {
      max_num = num,
      min_num = num,
      skill_name = baisi.name,
      include_equip = true,
      cancelable = false,
      prompt = "#doublez__baisi_discard"
    })
  end
})

return baisi